package com.cool4j.generator.builder

import com.cool4j.generator.entity.Table
import com.cool4j.generator.util.DateUtil
import com.cool4j.view.entity.HandlerVO
import freemarker.template.Configuration

/**
 * 构造器基础抽象类
 *
 * @author Frank create on 16/1/8 08:42
 */
abstract class BaseBuilder {

    /**
     * 生成代码
     *
     * @param table - 数据库表对象
     * @param packagePath - 包路径
     * @param handlerVO - 代码生成器界面设置内容
     * @param config - 模板配置对象
     * @return 是否生成成功
     */
    abstract boolean builder(Table table, String packagePath, HandlerVO handlerVO, Configuration config);

    /**
     * 构造模板需要的数据
     *
     * @param table - 数据表对象
     * @param packagePath - 包路径
     * @param handlerVO - 代码生成器界面设置内容
     * @return - 模板需要的数据
     */
    protected def getDataMap(Table table, String packagePath, HandlerVO handlerVO) {
        def dataMap = new HashMap<String, Object>()
        dataMap.put("table", table)
        dataMap.put("columns", table.getColumnList())
        dataMap.put("classPath", packagePath)
        dataMap.put("commonPackage", handlerVO.getCommonPackage())
        dataMap.put("author", handlerVO.getAuthor())
        dataMap.put("currentDate", DateUtil.currentDateForComment)
        dataMap.put("entitySuffix", handlerVO.getEntitySuffix())
        if (handlerVO.getMyBatis().getSelection()) {
            dataMap.put("braceStart", "#{")
            dataMap.put("braceEnd", "}")
        } else {
            dataMap.put("braceStart", "#")
            dataMap.put("braceEnd", "#")
        }
        dataMap.put("emailCharacter", "@")
        dataMap.put("phoneCharacter", "#")

        return dataMap
    }

}
